package com;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.util.ArrayList;

public class MyHbaseUtil {

    //获取连接
    public static Connection getConnection() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
        Connection connection = ConnectionFactory.createConnection(conf);
        return connection;
    }

    //创建表
    public static void createTable(String tableName, String... cls) throws Exception {
        //判断是否有列族
        if (cls.length <= 0) {
            System.out.println("请设置列族信息");
            return;
        }
        //2.判断表是否存在
        if (isTableExist(tableName)) {
            System.out.println(tableName + "表已经存在");
        }
        //创建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
        for (String column : cls) {
            //创建列族描述器
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(column);

            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        Admin admin = getAdmin();
        admin.createTable(hTableDescriptor);

    }


    //获取TABLE
    public static Table getTable(String tableName) throws Exception {
        Connection connection = getConnection();
        Table table = connection.getTable(TableName.valueOf(tableName));
        return table;
    }

    //获取ADMIN
    public static Admin getAdmin() throws Exception {
        return getConnection().getAdmin();
    }

    //展示表数据
    public static void printTableData(String tableName, String rowKey) throws Exception {
        Table table = getTable(tableName);
        Get get = new Get(rowKey.getBytes());
        Result row = table.get(get);
        while (row.advance()) {
            Cell cell = row.current();
            byte[] coumnFamily = CellUtil.cloneFamily(cell);
            byte[] coumn = CellUtil.cloneQualifier(cell);
            byte[] value = CellUtil.cloneValue(cell);
            System.out.println(Bytes.toString(coumnFamily) + ":" + Bytes.toString(coumn) + ",=" + Bytes.toString(value));
        }
        table.close();
    }
    //创建命名空间
    public static void createNameSpace(String str) throws Exception {
        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(str).build();
        Admin admin = getAdmin();
        admin.createNamespace(namespaceDescriptor);
    }


    private static boolean isTableExist(String tableName) throws Exception {
        Connection connection = getConnection();
        Table table = connection.getTable(TableName.valueOf(tableName));
        System.out.println(table);
        if (table == null) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) throws Exception {
//        Admin admin = MyHbaseUtil.getAdmin();
//        MyHbaseUtil.printTableData("stu", "1001");
//        System.out.println(admin.getConnection());
//
//        MyHbaseUtil.createTable("yang", "column1","column2");

        ArrayList<String> arr=new ArrayList<>(10);
        arr.add("张三");
        arr.add("李四");
        arr.add("王五");
        arr.add(1,"小六子");
        System.out.println(arr);

        MyHbaseUtil.isTableExist("yang");
    }


}
